package www.reagnetbox.com.scan;

import android.content.Context;
import android.hardware.usb.UsbDevice;


import java.io.IOException;

public class CrvScannerDevice {
	private final int VCOM_PID = 0x5011;
	private final int HIDPOS_PID = 0x0922;
	private final int CRV_VID = 0x0416;
	private Thread thread;
	private CommManage manage;
	private Context mContext;
	private boolean bHidPosMode;
	private boolean bVcomMode;
	private IScannerListener mListener;
	private CommAbility ability = null;

	private void startrcv() {
		if (thread != null) {
			return;
		}
		thread = new Thread(new Runnable() {
			@Override
			public void run() {
				byte[] data = null;
				int lens = 0;
				try {
					do {
						data = ability.recvDataLock(3000);
						if (data != null) {
							if (mListener != null) {
								mListener.onRecv(data);
							}
						}
					} while (ability != null && ability.isConnected);
				} catch (Exception ex) {
					thread = null;
				}
			}
		});
		thread.start();
	}

	public CrvScannerDevice(Context ctx, IScannerListener listener) {
		mContext = ctx;
		mListener = listener;
		manage = CommManage.getinstance();
		//打开log
		//manage.setLogEnable(true);
		//绑定监听context
		manage.registerContext(mContext);
		manage.addListener(new CommUtilityDelegate() {
			@Override
			public void onUsbPermission(UsbDevice device, boolean granted) {
				LogComm.printf("pid: " + device.getProductId()
						+ " vid:" + device.getVendorId() + " granted:" + granted);
				if (granted) {
					if (device.getProductId() == VCOM_PID && device.getVendorId() == CRV_VID) {
						bVcomMode = true;
						bHidPosMode = false;
						if (mListener != null) {
							mListener.onPermissonGranted();
						}
					} else if (device.getProductId() == HIDPOS_PID && device.getVendorId() == CRV_VID) {
						bVcomMode = false;
						bHidPosMode = true;
						if (mListener != null) {
							mListener.onPermissonGranted();
						}
					}
				}
			}

			@Override
			public void onUsbAttach(UsbDevice device) {
				LogComm.printf("onUsbAttach device:" + device);
				if (device.getProductId() == VCOM_PID && device.getVendorId() == CRV_VID) {
					bVcomMode = true;
					bHidPosMode = false;
					if (mListener != null) {
						mListener.onAttach();
					}
				} else if (device.getProductId() == HIDPOS_PID && device.getVendorId() == CRV_VID) {
					bVcomMode = false;
					bHidPosMode = true;
					if (mListener != null) {
						mListener.onAttach();
					}
				}
			}

			@Override
			public void onUsbDettach(UsbDevice device) {
				if (device.getProductId() == VCOM_PID && device.getVendorId() == CRV_VID) {
					bVcomMode = false;
					if (mListener != null) {
						mListener.onDettach();
					}
				} else if (device.getProductId() == HIDPOS_PID && device.getVendorId() == CRV_VID) {
					bHidPosMode = false;
					if (mListener != null) {
						mListener.onDettach();
					}
				}
			}
		});

	}

	public void setVcomMode() {
		bVcomMode = true;
		bHidPosMode = false;
	}

	public void setHidPosMode() {
		bHidPosMode = true;
		bVcomMode = false;
	}

	/**
	 * 通讯是否虚拟串口模式
	 *
	 * @return
	 */
	public boolean isVcomMode() {
		return bVcomMode;
	}

	/**
	 * 通讯是否hidpos模式
	 *
	 * @return
	 */
	public boolean isHidPosMode() {
		return bHidPosMode;
	}

	public boolean isOpened() {
		if (ability != null && ability.isConnected) {
			return true;
		}
		return false;
	}

	public boolean open() throws IOException, RuntimeException {
		if (ability != null && ability.isConnected) {
			return true;
		}
		if (!bHidPosMode && !bVcomMode) {
			return false;
		}

		try {
			if (ability == null) {
				UsbCommDev usbDev;
				if (bVcomMode) {
					usbDev = new UsbCommDev(String.valueOf(VCOM_PID), String.valueOf(CRV_VID),
							"10", "0", "0");
					usbDev.setDescript(CommTypeDescript.USB_CDC);
				} else {
					usbDev = new UsbCommDev(String.valueOf(HIDPOS_PID), String.valueOf(CRV_VID),
							"3", "0", "0");
					usbDev.setDescript(CommTypeDescript.USB_HID);
				}

				ability = manage.getComm(usbDev);
			}
			if (ability != null) {
				ability.open();
				//开启接收线程

				if (ability.isConnected) {
					LogComm.printf(" ability open suc");
					startrcv();
					return true;
				}
			}
		} catch (RuntimeException ex) {
			throw new RuntimeException(ex);
		} catch (IOException ex) {
			throw new IOException(ex);
		}
		LogComm.printf(" ability open fail");
		return false;
	}

	public void close() {
		if (ability != null) {

			LogComm.printf("------------usbClose-----------");
			try {
				ability.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			ability = null;
		}
		thread = null;
	}

	/**
	 * 关闭设备，unreg 监听
	 */
	public void destroy() {
		close();
		manage.unregisterContext();
	}

	/**
	 * 发送接口
	 *
	 * @param data
	 * @return 实际发送长度
	 */
	public int send(byte[] data) {
		if (ability != null && data != null && data.length > 0 && ability.isConnected) {
			return ability.sendData(data);
		}
		return 0;
	}


	/**
	 * 命令转为hidpos，再发送
	 *
	 * @param data
	 * @return 实际发送长度
	 */
	public int sendByHidPos(byte[] data) {
		data = CmdUtils.toHidPos(data);
		if (ability != null && data != null && data.length > 0 && ability.isConnected) {
			return ability.sendData(data);
		}
		return 0;
	}
}
